//
//  DatumViewController.m
//  LoLBox
//
//  Created by lanou3g on 15/5/30.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DatumViewController.h"

@interface DatumViewController ()

@property (nonatomic , strong) NSMutableArray * dataArray;

@property (nonatomic , strong) ShowViewController * showVc;

@property (nonatomic , strong) NSMutableArray * dataList;

@property (nonatomic , strong) NSMutableArray * searchList;

@end

@implementation DatumViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self sendUrl];
    
    [self settings];
    
    [self searchBar];
    
}

-(void)settings
{

    
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"niuniu.png"]];
    
    imageView.frame = CGRectMake(0, 0, 375, 675);
    
    [self.view addSubview: imageView];
    
    [self.view sendSubviewToBack:imageView];
    
    self.collectionVc.backgroundColor = [UIColor colorWithRed:(250.0/255.0) green:(255.0/255.0) blue:(255.0/255.0) alpha:0.1];
    
    self.navigationItem.title = @"英雄资料";
    
    self.collectionVc.delegate = self;
    
    self.collectionVc.dataSource = self;
    
    UIBarButtonItem * leftBI = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(wayBI)];
    
    self.navigationItem.leftBarButtonItem = leftBI;
    
    
    [self.collectionVc registerClass:[MyCell class] forCellWithReuseIdentifier:@"datumcell"];
}


-(void)searchBar
{
    //透明搜索框
    for (UIView *view in self.search.subviews) {
        
        if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [view removeFromSuperview];
            break;
        }
        if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
            [[view.subviews objectAtIndex:0] removeFromSuperview];
            break;
        }
    }
/*
    //[self.search setSearchResultsButtonSelected:YES];//设置搜索结果按钮是否选中
 
    //[self.search setShowsSearchResultsButton:YES];//是否显示搜索结果按钮
 
    //设置搜索框下边的分条栏
    //[self.search setShowsScopeBar:YES];//是否显示分条栏
 
   // [self.search setScopeButtonTitles:[NSArray arrayWithObjects:@"你MA",@"NIBA",@"艹", nil]];
 
    //[self.search setScopeBarBackgroundImage:[UIImage imageNamed:@"Janna"]];//分条栏背景
 
    //[self.search setSelectedScopeButtonIndex:1];//分条栏默认选中的按钮下标
 
 */
    
    UIViewController * viewC = [[UIViewController alloc]init];
    
    viewC.view.frame= CGRectMake(0, 0, 100, 100);
    
    viewC.view.backgroundColor = [UIColor orangeColor];
    
    UISearchController * searchC = [[UISearchController alloc]initWithSearchResultsController:viewC];
    

    
    searchC.searchResultsUpdater = viewC;
    
    self.definesPresentationContext = YES;
    
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    
    
    
    
    return YES;
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{

    [self.search resignFirstResponder];
    
    [self.search setShowsCancelButton:NO animated:YES];
    
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
 }





-(void)wayBI
{
  
    [self.navigationController popViewControllerAnimated:YES];

}


-(void)sendUrl
{
    self.dataArray = [NSMutableArray array];
    
    NSURL * url = [NSURL URLWithString:@"http://ossweb-img.qq.com/upload/qqtalk/lol_hero/hero_list.js"];
    
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    
    __weak DatumViewController * datumVC = self;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (data == nil) {
            
            return ;
            
        }
        
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary * dic  in array) {
            
            ModleCell * modle = [[ModleCell alloc]init];
            
            [modle setValuesForKeysWithDictionary:dic];
            
            [datumVC.dataArray addObject:modle];
            
        }
        
            [self.collectionVc reloadData];
    }];



}


//collectionCell展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"datumcell" forIndexPath:indexPath];
    
    cell.opaque = NO;
    
    cell.selectedBackgroundView =  [[UIImageView alloc]init];
    
    cell.selectedBackgroundView.backgroundColor = [UIColor cyanColor];
    
    ModleCell * modle = _dataArray[indexPath.row];
    
    cell.modle = modle;
    
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
     [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    ModleCell * modle = _dataArray[indexPath.item];
    //NSLog(@"%ld",(long)indexPath.row);
    
    self.showVc= [[ShowViewController alloc]init];
    
    _showVc.str = modle.ID;
    
    [self.navigationController pushViewController:_showVc animated:YES];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _dataArray.count;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
